//app.js
import {
    http
} from "./common/http";
App({
    $http: http,
    updated:0,
    onLaunch: async function () {
        this.update();
        wx.cloud.init({
            env: 'cloud1-2gtfsnq496c2bb9d'
        });
        this.globalData = {}
    },
    async onShow(res) {
        this.update();
    },
    // 小程序更新
    update() {
        if(this.updated) return;
        const UpdateManager = wx.getUpdateManager();
        UpdateManager.onUpdateReady(res => {
            this.updated=1;
            this.showModal("正在更新版本",() => {
                UpdateManager.applyUpdate();
                this.updated=0;
            });  
        });
    },
    getPage(index) {
        let pages = getCurrentPages();
        let cur = pages[pages.length + (index - 1)];
        return cur;
    },
    showModal(content, fn) {
        wx.showModal({
            title: '提示',
            showCancel: fn?true:false,
            confirmText: fn?'确定':"我知道了",
            confirmColor:'#244BE4',
            content: String(content) || '发生错误，一年后修理',
            success(res) {
                if (res.confirm && fn) {
                    fn();
                } else if (res.cancel) {
                    console.log('用户点击取消')
                }
            }
        });
    },
    showLoading() {
        let sys = wx.getSystemInfoSync();
        let isIphone = /^iPhone/.test(sys.model);
        wx.showLoading({
            title: isIphone ? '' : '请稍等',
            mask: true
        });
    }
})